// setStorage、getStorage

/**
 * 存储localStorage
 */
export const setStore = (params) => {
  const { name, content, type } = params
  const obj = {
    dataType: typeof (content),
    content,
    type,
    datetime: new Date().getTime()
  }
  if (type) window.sessionStorage.setItem(name, JSON.stringify(obj))
  else window.localStorage.setItem(name, JSON.stringify(obj))
}
/**
 * 获取localStorage
 */
export const getStore = (params) => {
  const { name } = params
  let item = window.localStorage.getItem(name)
  if (!item) {
    item = window.sessionStorage.getItem(name)
  }
  if (item) {
    const obj = JSON.parse(item)
    let content = {}
    if (obj.dataType === 'string') {
      content = obj.content
    } else if (obj.dataType === 'number') {
      content = Number(obj.content)
    } else if (obj.dataType === 'boolean') {
      content = eval(obj.content)
    } else if (obj.dataType === 'object') {
      content = obj.content
    }
    return content
  } else {
    return {}
  }
}
/**
 * 删除localStorage
 */
export const removeStore = (params) => {
  const { name } = params
  window.localStorage.removeItem(name)
  window.sessionStorage.removeItem(name)
}
